package com.chj.design.controller;

import com.chj.design.handler.ChainPatternDemo;
import com.chj.design.observer.EventBusCenter;
import com.chj.design.observer.EventListener;
import com.chj.design.observer.NotifyEvent;
import com.chj.design.strategy.StrategyUseService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/demo")
public class DemoController {

    @Resource
    ChainPatternDemo chainPatternDemo;

    @Resource
    StrategyUseService strategyUseService;

    @Resource
    EventListener eventListener;

    @Resource
    EventBusCenter eventBusCenter;

    /**
     * 责任链模式
     *
     * @return
     */
    @GetMapping("/chain")
    public String chain() {
        return chainPatternDemo.exec("request", "reponse");
    }

    /**
     * 策略模式
     *
     * @param fileType
     */
    @GetMapping("/strategy")
    public void strategy(String fileType) {
        strategyUseService.resolveFile(fileType, "strategy");
    }

    @GetMapping("/observer")
    public void observer() {
        eventBusCenter.register(eventListener);
        eventBusCenter.post(new NotifyEvent("13372817283", "123@qq.com", "666"));
    }
}
